//
//  NewFeatureViewController.swift
//  weibo19
//
//  Created by Arvin on 15/11/12.
//  Copyright © 2015年 Arvin. All rights reserved.
//

import UIKit

private let reuseIdentifier = "NewFeatureViewCell"
private let pictureCount = 4

class NewFeatureViewController: UICollectionViewController {
    // 在构造方法中实例化一个layout对象, 然后调用super的构造方法
    init() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen .mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // collectionView上添加pageControl, pageC只能在第一个页面显示, 这个cv很特殊.
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(NewFeatureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return pictureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureViewCell
        
        cell.pageIndex = indexPath.item
        return cell
    }
    
    private var page: Int = 0
    override func scrollViewDidScroll(scrollView: UIScrollView) {
        page = (Int)(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width + 0.5)
        
        if page == pictureCount - 1 {
            let indexPath = NSIndexPath(forItem: page, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! NewFeatureViewCell
            cell.showStartBtnAnimation()
        }
    }
}

class NewFeatureViewCell: UICollectionViewCell {
    
    private var pageIndex: Int = 0 {
        didSet {
            print(pageIndex)
            backView.image = UIImage(named: "new_feature_" + "\(pageIndex + 1)")
            pagControl.currentPage = pageIndex
            startBtn.hidden = true
        }
    }
    
    private func showStartBtnAnimation() {
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        // Damping值0~1 越小, 弹力越大
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.94, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            self.startBtn.hidden = false
            self.startBtn.transform = CGAffineTransformIdentity
            }, completion: { (end) -> Void in
                print(end)
        })
    }
    // MARK: 按钮点击
    @objc func didStartStatus() {
        NSNotificationCenter.defaultCenter().postNotificationName(switchController, object: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var backView: UIImageView = UIImageView()
    lazy var startBtn: UIButton = {
        let start = UIButton(title: "开始微博", imageN: "new_feature_finish_button", imageH: "new_feature_finish_button_highlighted", color: UIColor.whiteColor())
        start.addTarget(self, action: "didStartStatus", forControlEvents: UIControlEvents.TouchUpInside)
        return start
    }()
    
    lazy var pagControl: UIPageControl = {
        let page = UIPageControl()
        page.numberOfPages = pictureCount
        page.currentPage = 0
        page.currentPageIndicatorTintColor = UIColor.orangeColor()
        page.pageIndicatorTintColor = UIColor.grayColor()
        return page
        }()

    private func addSubUI() {
        contentView.addSubview(backView)
        backView.addSubview(startBtn)
        backView.userInteractionEnabled = true
        backView.frame = bounds
        
        startBtn.hidden = true
        startBtn.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
            make.centerX.equalTo(backView.snp_centerX)
        }
        
        backView.addSubview(pagControl)
        pagControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(backView.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.85)
            make.width.equalTo(200)
            make.height.equalTo(30)
        }
    }
}
